﻿Public Class frmMain
    Dim MDIFormCompany As New frmCompany
    Dim MDIFormProduct As New frmProduct
    Dim MDIFormAccount As New frmAccount
    Dim MDIFormComCode As New frmComCode
    Dim MDIFormIncome As New frmIncome
    Dim MDIFormPayment As New frmPayment
    Dim MDIFormPrdInOut As New frmPrdInOut
    Dim MDIFormTradeBill As New frmTradeBill
    Dim MDIFormStatAcc As New frmStatAcc
    Dim MDIFormStatComp As New frmStatComp
    Dim MDIFormMember As New frmMember
    Dim MDIFormStatPrdQty As New frmStatPrdQty
    Dim sql As String
    Dim dsMain As DataSet
    Dim daMain As OleDb.OleDbDataAdapter

    Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        Dim result As Integer = MessageBox.Show("인벤토리맨을 종료 하시겠습니까?", "InventoryMan", MessageBoxButtons.YesNo)
        If result = DialogResult.No Then
            e.Cancel = True
        ElseIf result = DialogResult.Yes Then
            Call gSubDataBackup("")
            frmCompany.Close()
            frmProduct.Close()
            frmAccount.Close()
            frmComCode.Close()
            frmIncome.Close()
            frmPayment.Close()
            frmPrdInOut.Close()
            frmTradeBill.Close()
            frmStatAcc.Close()
            frmStatComp.Close()
            frmStatPrdQty.Close()
            frmMember.Close()
            frmLogin.Close()
            frmJoinMem.Close()

            dbConn.Close()
            dbConnSQL.Close()
        End If
    End Sub

    Private Sub 거래처관리ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 거래처관리ToolStripMenuItem.Click
        Dim tbPage As String = "거래처관리"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormCompany, tbPage)
    End Sub

    Private Sub 제품관리ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 제품관리ToolStripMenuItem.Click
        Dim tbPage As String = "제품관리"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormProduct, tbPage)
    End Sub

    Private Sub 계정관리ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 계정관리ToolStripMenuItem.Click
        Dim tbPage As String = "계정관리"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormAccount, tbPage)
    End Sub

    Private Sub 공통코드ToolStripMenuItem_Click_1(sender As System.Object, e As System.EventArgs) Handles 공통코드ToolStripMenuItem.Click
        Dim tbPage As String = "공통코드"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormComCode, tbPage)
    End Sub

    Private Sub 매출ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 매출ToolStripMenuItem.Click
        Dim tbPage As String = "매출"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormIncome, tbPage)
    End Sub

    Private Sub 매입ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 매입ToolStripMenuItem.Click
        Dim tbPage As String = "매입"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormPayment, tbPage)
    End Sub

    Private Sub 입출고관리ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 입출고관리ToolStripMenuItem.Click
        Dim tbPage As String = "입출고관리(입고)"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormPrdInOut, tbPage)
    End Sub

    Private Sub 거래명세서ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles 거래명세서ToolStripMenuItem.Click
        Dim tbPage As String = "거래명세서"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormTradeBill, tbPage)
    End Sub

    Private Sub 계정ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles 계정ToolStripMenuItem.Click
        Dim tbPage As String = "매입매출(계정)"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormStatAcc, tbPage)
    End Sub

    Private Sub 회사ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 회사ToolStripMenuItem.Click
        Dim tbPage As String = "매입매출(거래처)"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormStatComp, tbPage)
    End Sub

    Private Sub 회원관리ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 회원관리ToolStripMenuItem.Click
        Dim tbPage As String = "회원관리"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormMember, tbPage)
    End Sub

    Private Sub 제품판매ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 제품판매ToolStripMenuItem.Click

        gSessionKey = ""
        Dim MDIFormSales As New frmSales
        MDIFormSales.Show()
        Call initMainMent()
    End Sub

    Private Sub 입고출현황ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles 입출고현황ToolStripMenuItem.Click
        Dim tbPage As String = "상품현황(입출고)"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormStatPrdQty, tbPage)
    End Sub

    Private Sub InfoToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles InfoToolStripMenuItem.Click
        frmVersion.Show()
    End Sub

    Private Sub 창크기초기화ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles 창크기초기화ToolStripMenuItem.Click
        Me.Size = New System.Drawing.Size(1100, 750)
    End Sub

#Region "라이브러리"
    Private Sub initMainMent()
        mainMenu.Visible = False
        mainMenu.Visible = True
        mainMenu.ResumeLayout()
    End Sub
#End Region

    Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Call gSubIconShow(Me)

        Dim os As OperatingSystem = Environment.OSVersion
        If Mid(os.ToString, 1, 22) = "Microsoft Windows NT 5" Then
            Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
        End If
        'Call gridRefresh()
        mainTab.Visible = False
    End Sub

    Private Sub 월간보고서ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles 월간보고서ToolStripMenuItem.Click

        Dim MDIFormDisplay As New frmDisplay
        MDIFormDisplay.Show()
        Call initMainMent()
    End Sub

    Private Sub mLeftCompany_Click(sender As System.Object, e As System.EventArgs) Handles mLeftCompany.Click
        Dim tbPage As String = "거래처관리"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormCompany, tbPage)
    End Sub

    Private Sub mLeftMember_Click(sender As System.Object, e As System.EventArgs) Handles mLeftMember.Click
        Dim tbPage As String = "회원관리"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormMember, tbPage)
    End Sub

    Private Sub mLeftProduct_Click(sender As System.Object, e As System.EventArgs) Handles mLeftProduct.Click
        Dim tbPage As String = "제품관리"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormProduct, tbPage)
    End Sub

    Private Sub mLeftSales_Click(sender As System.Object, e As System.EventArgs) Handles mLeftSales.Click

        gSessionKey = ""
        Dim MDIFormSales As New frmSales
        MDIFormSales.Show()
        Call initMainMent()
    End Sub

    Private Sub mLeftInOut_Click(sender As System.Object, e As System.EventArgs) Handles mLeftInOut.Click
        Dim tbPage As String = "입출고관리(입고)"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormPrdInOut, tbPage)
    End Sub

    Private Sub mLeftTradeBill_Click(sender As System.Object, e As System.EventArgs) Handles mLeftTradeBill.Click
        Dim tbPage As String = "거래명세서"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormTradeBill, tbPage)
    End Sub

    Private Sub mLeftIncome_Click(sender As System.Object, e As System.EventArgs) Handles mLeftIncome.Click
        Dim tbPage As String = "매출"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormIncome, tbPage)
    End Sub

    Private Sub mLeftPayment_Click(sender As System.Object, e As System.EventArgs) Handles mLeftPayment.Click
        Dim tbPage As String = "매입"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormPayment, tbPage)
    End Sub

    Private Sub mLeftAccount_Click(sender As System.Object, e As System.EventArgs) Handles mLeftAccount.Click
        Dim tbPage As String = "계정관리"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormAccount, tbPage)
    End Sub

    Private Sub mLeftStatAccount_Click(sender As System.Object, e As System.EventArgs) Handles mLeftStatAccount.Click
        Dim tbPage As String = "매입매출(계정)"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormStatAcc, tbPage)
    End Sub

    Private Sub mLeftStatCompany_Click(sender As System.Object, e As System.EventArgs) Handles mLeftStatCompany.Click
        Dim tbPage As String = "매입매출(거래처)"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormStatComp, tbPage)
    End Sub

    Private Sub mLeftStatProduct_Click(sender As System.Object, e As System.EventArgs) Handles mLeftStatProduct.Click
        Dim tbPage As String = "상품현황(입출고)"
        If fnExistTab(tbPage) = True Then
            Exit Sub
        End If
        mkFormIntoTab(MDIFormStatPrdQty, tbPage)
    End Sub

    Private Sub mLeftReportMonth_Click(sender As System.Object, e As System.EventArgs) Handles mLeftReportMonth.Click

        Dim MDIFormDisplay As New frmDisplay
        MDIFormDisplay.Show()
        Call initMainMent()
    End Sub

    Private Sub 백업복구ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles 백업복구ToolStripMenuItem.Click

        Dim MDIFormBackup As New frmBackup
        MDIFormBackup.Show()
        Call initMainMent()
    End Sub

    Private Sub 바로가기보이기ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles 바로가기보이기ToolStripMenuItem.Click
        leftPanel.Width = 150
        GroupBox1.Dock = DockStyle.Fill
        Panel5.Visible = False
        Panel6.Visible = False
        Panel1.Visible = True
        Panel2.Visible = True
        Panel3.Visible = True
    End Sub

    Private Sub PicBox_Click(sender As System.Object, e As System.EventArgs) Handles PicBox.Click
        leftPanel.Width = 24

        Panel1.Visible = False
        Panel2.Visible = False
        Panel3.Visible = False

        Panel5.Visible = True
        Panel5.BringToFront()
        Panel6.Visible = True
        Panel5.Dock = DockStyle.Fill
        GroupBox1.Dock = DockStyle.Fill
    End Sub

    Private Sub PicBox_MouseHover(sender As Object, e As System.EventArgs) Handles PicBox.MouseHover
        PicBox.Image = My.Resources.leftClose_o
    End Sub

    Private Sub PicBox_MouseLeave(sender As Object, e As System.EventArgs) Handles PicBox.MouseLeave
        PicBox.Image = My.Resources.leftClose
    End Sub

#Region "라이브러리"

    'Private Sub gridRefresh()
    '    Call dbSqlConnection()
    '    Dim addSql As String = ""

    '    dsMain = New DataSet

    '    sql = "SELECT top 100  seq, latestVersion  Version, updateMsg 내용, addDt 일자 " & _
    '        "   FROM InventoryManInfo WHERE flag='Y'" + addSql + " ORDER BY addDt DESC"
    '    'MsgBox(sql)

    '    Try
    '        daMain = New OleDb.OleDbDataAdapter(sql, dbConnSQL)
    '        daMain.Fill(dsMain, "Update")

    '        grdUpdate.DataSource = dsMain.Tables("Update")

    '        dbConnSQL.Close()
    '        Call setGridDesign()

    '    Catch err As ObjectDisposedException
    '        dbConnSQL.Close()
    '        MsgBox(sql)

    '        MsgBox(err.Message, MsgBoxStyle.Critical, "오류")
    '    End Try
    'End Sub

    'Private Sub setGridDesign()

    '    grdUpdate.Columns(0).Visible = False
    '    grdUpdate.Columns(1).Width = "80"
    '    grdUpdate.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    '    grdUpdate.Columns(2).Width = "412"
    '    grdUpdate.Columns(3).Width = "138"

    '    With grdUpdate.DefaultCellStyle
    '        .Alignment = DataGridViewContentAlignment.MiddleLeft
    '    End With

    'End Sub
    'Private Sub grdUpdate_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
    '    If e.RowIndex = -1 Then
    '        Exit Sub
    '    End If
    '    If IsDBNull(grdUpdate.Rows(e.RowIndex).Cells(0).Value) = True Then
    '        Exit Sub
    '    End If
    '    sql = "SELECT latestVersion, updateMsg, addDt  FROM InventoryManInfo WHERE seq=" + grdUpdate.Rows(e.RowIndex).Cells(0).Value.ToString

    '    Call dbSqlConnection()
    '    dsMain = New DataSet
    '    daMain = New OleDb.OleDbDataAdapter(sql, dbConnSQL)
    '    daMain.Fill(dsMain, "updateInfo")

    '    dbConnSQL.Close()

    '    txtReset()

    '    lbVersion.Text = dsMain.Tables("updateInfo").Rows(0).Item(0)
    '    lbWdate.Text = dsMain.Tables("updateInfo").Rows(0).Item(2)
    '    txtUpdateMsg.Text = dsMain.Tables("updateInfo").Rows(0).Item(1)
    'End Sub

    'Private Sub txtReset()
    '    lbVersion.Text = ""
    '    lbWdate.Text = ""
    '    txtUpdateMsg.Clear()
    'End Sub

    '## Form 그리기
    Private Sub mkFormIntoTab(ByRef obj As Object, ByVal tbPage As String)

        Dim tabPage As New TabPage
        Dim posit As Integer = mainTab.TabPages.Count

        If mainTab.Visible = False Then
            mainTab.Visible = True
        End If
        tabPage.Text = tbPage

        obj.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        obj.Dock = DockStyle.Fill
        obj.TopLevel = False

        mainTab.Controls.Add(tabPage)
        obj.Parent = mainTab.TabPages(posit)

        obj.Show()
        obj.BringToFront()
        mainTab.SelectedIndex = posit
    End Sub

    '## Tab 유무 확인
    Private Function fnExistTab(ByVal pageText As String) As Boolean

        Dim isTab As Boolean = False
        For k As Integer = 0 To mainTab.TabPages.Count - 1
            If mainTab.TabPages(k).Text = pageText Then

                mainTab.SelectedIndex = k
                isTab = True
            End If
        Next
        Return isTab

    End Function
#End Region

    Private Sub PicBox2_Click(sender As System.Object, e As System.EventArgs) Handles PicBox2.Click
        leftPanel.Width = 150
        GroupBox1.Dock = DockStyle.Fill
        Panel5.Visible = False
        Panel6.Visible = False
        Panel1.Visible = True
        Panel2.Visible = True
        Panel3.Visible = True
    End Sub

    Private Sub 환경설정ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles 환경설정ToolStripMenuItem.Click

        Dim MDIFormConfig As New frmConfig
        frmConfig.Show()
        Call initMainMent()
    End Sub
End Class